﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Inheritance</title>   
</head>
    <body>
        <pre>
            We start by making a function that will produce objects. We will give it a name that starts with a lowercase letter because it will not require the use of the new prefix. The function contains four steps:

            It creates a new object. There are lots of ways to make an object. It can make an object literal, or it can call a constructor function with the new prefix, or it can use the Object.beget method to make a new instance from an existing object, or it can call any function that returns an object.

            It optionally defines private instance variables and methods. These are just ordinary vars of the function.

            It augments that new object with methods. Those methods will have privileged access to the parameters and the vars defined in the second step.

            It returns that new object.

            Here is a pseudocode template for a functional constructor (boldface text added for emphasis):

            var constructor = function (spec, my) {
                var that, other private instance variables;
                my = my || {};

                Add shared variables and functions to my

                that = a new object;

                Add privileged methods to that

                return that;
            }
            <strong>ver pag 45 del libro</strong>
        </pre>
        <script type="text/javascript" src="jsExtensions.js"></script>
        <script type="text/javascript">
            var initialization = function () {
                var mammalDefinition = function () {
                    Mammal = function (name) {
                        this.name = name;
                    };
                    Mammal.prototype.get_name = function () {
                        return this.name;
                    };
                    Mammal.prototype.says = function () {
                        return this.saying || '';
                    };
                }();
            }();
            var inheritance = {
                pseudoclassical_FirstApproach_father: function () {
                    var myMammal = new Mammal('Herb the Mammal');
                    return myMammal.get_name() == 'Herb the Mammal';
                }(),
                pseudoclasical_FirstApproach_son: function () {
                    var catDefinition = function () {
                        Cat = function (name) {
                            this.name = name;
                            this.saying = 'meow';
                        };

                        // Replace Cat.prototype with a new instance of Mammal
                        Cat.prototype = new Mammal();

                        // Augment the new prototype with
                        // purr and get_name methods.
                        Cat.prototype.purr = function (n) {
                            var i, s = '';
                            for (i = 0; i < n; i += 1) {
                                if (s) {
                                    s += '-';
                                }
                                s += 'r';
                            }
                            return s;
                        };
                        Cat.prototype.get_name = function () {
                            return this.says() + ' ' + this.name + ' ' + this.says();
                        };

                    }();
                    var myCat = new Cat('Henrietta');
                    return myCat.says() == 'meow' &&
                        myCat.purr(5) == 'r-r-r-r-r' &&
                        myCat.get_name() == 'meow Henrietta meow';
                }(),
                pseudoclasical_best: function () {
                    var Cat = function (name) {
                            this.name = name;
                            this.saying = 'meow';
                        }.inherits(Mammal).
                        method('purr', function (n) {
                            var i, s = '';
                            for (i = 0; i < n; i += 1) {
                                if (s) {
                                    s += '-';
                                }
                                s += 'r';
                            }
                            return s;
                        }).method('get_name', function () {
                            return this.says() + ' ' + this.name +
                                ' ' + this.says();
                        });
                },
                prototypal: function () {
                    var myMammal = {
                        name: 'Herb the Mammal',
                        get_name: function () {
                            return this.name;
                        },
                        says: function () {
                            return this.saying || '';
                        }
                    };

                    var myCat = Object.beget(myMammal);
                    myCat.name = 'Henrietta';
                    myCat.saying = 'meow';
                    myCat.purr = function (n) {
                        var i, s = '';
                        for (i = 0; i < n; i += 1) {
                            if (s) {
                                s += '-';
                            }
                            s += 'r';
                        }
                        return s;
                    };
                    myCat.get_name = function () {
                        return this.says() + ' ' + this.name + ' ' + this.says();
                    };

                    return myCat.name == 'Henrietta' &&
                        myCat.purr(2) == 'r-r' &&
                        myCat.get_name() == 'meow Henrietta meow';
                }(),
                functional: function () {
                    var mammal = function (spec) {
                        var that = {};

                        that.get_name = function () {
                            return spec.name;
                        };

                        that.says = function () {
                            return spec.saying || '';
                        };

                        return that;
                    };

                    var myMammal = mammal({ name: 'Herb' });

                    var ok = myMammal.get_name() == 'Herb' &&
                        myMammal.says() == '';

                    var cat = function (spec) {
                        spec.saying = spec.saying || 'meow';
                        var that = mammal(spec);
                        that.purr = function (n) {
                            var i, s = '';
                            for (i = 0; i < n; i += 1) {
                                if (s) {
                                    s += '-';
                                }
                                s += 'r';
                            }
                            return s;
                        };
                        that.get_name = function () {
                            return that.says() + ' ' + spec.name + ' ' + that.says();
                        }
                        return that;
                    };

                    var myCat = cat({ name: 'Henrietta' });

                    return ok && myCat.get_name() == 'meow Henrietta meow' &&
                        myCat.purr(1) == 'r';
                }(),                
            };
        </script>
    </body>
</html>
